<?php
App::uses('AppController', 'Controller');
/**
 * Adverts Controller
 *
 * @property Advert $Advert
 * @property PaginatorComponent $Paginator
 */
class AdvertsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Advert->recursive = 0;
		$this->set('adverts', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Advert->exists($id)) {
			throw new NotFoundException(__('Invalid advert'));
		}
		$options = array('conditions' => array('Advert.' . $this->Advert->primaryKey => $id));
		$this->set('advert', $this->Advert->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function admin_add() {
        if ($this->request->is('post')) {
            $this->Advert->create();
            $files = $this->request->data['Advert']['image'];
            move_uploaded_file($files['tmp_name'], WWW_ROOT . '/img/frontend/advert/' . $files['name']);
             $this->request->data['Product']['create_date'] = date("Y-m-d");
            $this->request->data['Product']['create_person'] = $this->Session->read("userName");
            
            $this->request->data['Advert']['image'] = $this->webroot . "img/frontend/advert/" . $files['name'];
            if ($this->Advert->save($this->request->data)) {
                $this->Session->setFlash(__('Thêm thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Could not be saved. Please, try again.'));
            }
        }
    }

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
        if (!$this->Advert->exists($id)) {
            throw new NotFoundException(__('Invalid banner'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $files = $this->request->data['Advert']['image'];
            move_uploaded_file($files['tmp_name'], WWW_ROOT . '/img/frontend/advert/' . $files['name']);
            $this->request->data['Advert']['create_date'] = date("Y-m-d");
            $this->request->data['Advert']['image'] = $this->webroot . "img/frontend/advert/" . $files['name'];
            if ($this->Advert->save($this->request->data)) {
                $this->Session->setFlash(__('Cập nhật thành công '), 'backend/green', array('redirect' => ''));
                return $this->redirect(array('action' => 'admin_index'));
            } else {
                $this->Session->setFlash(__('Could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Advert.' . $this->Advert->primaryKey => $id));
            $this->request->data = $this->Advert->find('first', $options);
        }
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	
        public function admin_delete($id = null) {
        $this->Advert->id = $id;
        if (!$this->Advert->exists()) {
            throw new NotFoundException(__('Invalid banner'));
        }
        $this->request->onlyAllow('get', 'admin_delete');

        if ($this->Advert->delete()) {
            $this->Session->setFlash(__('Xóa thành công'), 'backend/green', array('redirect' => ''));
        } else {
            $this->Session->setFlash(__('Lỗi xóa dữ liệu'), 'backend/red', array('redirect' => ''));
        }
        return $this->redirect(array('action' => 'admin_index'));
    }
    public function getAdvert($typeId)
    {
        $adverts = $this->Advert->find('first',array('conditions'=>array('Advert.position'=>$typeId),'order'=>array('Advert.create_date DESC')));
        return $adverts;
    }
    
        
                }
